package cn.wen.yaling.controller;

import java.util.*;

import cn.wen.yaling.aop.annotation.operation.OperationLogger;
import cn.wen.yaling.aop.annotation.visit.BusinessLog;
import cn.wen.yaling.commons.enums.EBehavior;
import cn.wen.yaling.commons.vo.*;
import cn.wen.yaling.xo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.xo.service.ArticleService;
import cn.wen.yaling.commons.utils.ResultEntity;


/**
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    /**
     * 获取博客的详情信息 点击一个详情页面添加一个阅读
     *
     * @return
     */
    @BusinessLog(value = "点击博客", behavior = EBehavior.BLOG_CONTENT)
    @GetMapping("/web/article/detail-info")
    public ResultEntity getArticleInfoById(@RequestParam("articleId") Integer articleId) {
        /**
         * 博客详情中一般包括
         * 1、博客的内容详情
         * 2、博客的评论详情
         */
        // 文章的内容
        ArticleVO articleVO = articleService.getArticleInfoById(articleId);
        // 文章的评论列表
        List<CommentVO> commentList = commentService.listCommentByArticleId(articleId);

        Map<String, Object> map = new HashMap<>();
        map.put("articleInfo", articleVO);
        map.put("commentList", commentList);

        // TODO 需要添加一次浏览量 更新数据库数据  这里使用Redis来实现 数据库操作
        commentService.updateArticleViewsNum(articleId);
        return ResultEntity.ok().put("data", map);
    }

    /**
     * 获取归档的所有数据
     *
     * @return
     */
    @GetMapping("/web/archive/article-list")
    public ResultEntity getArchiveArticleList() {
        // 直接调用到业务层实现  实现年  ---  月日  实现接口
        List<ArchiveArticleVO> archiveArticleVOS = articleService.getArchiveArticleList();
        return ResultEntity.ok().put("data", archiveArticleVOS);
    }

    /**
     * =========================== 后台 ====================================
     **/

    @OperationLogger(value = "添加文章")
    @PostMapping("/admin/article/add")
    public ResultEntity addArticle(@RequestBody ArticleAddVO article) {
        boolean res = articleService.addArticle(article);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }


    /**
     * 修改发布状态
     *
     * @param articleId 需要修改的文章Id
     * @param isPublish 需要提交的发布状态
     * @return
     */
    @OperationLogger(value = "修改文章状态")
    @PostMapping("/admin/article/update-publish")
    public ResultEntity updatePublishStatus(@RequestParam("articleId") Integer articleId,
                                            @RequestParam("isPublish") Boolean isPublish) {
        boolean res = articleService.updateArticlePublishStatus(articleId, isPublish);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    /**
     * 修改发布状态
     *
     * @param articleId 需要修改的文章Id
     * @param status    需要提交的发布状态
     * @return
     */
    @OperationLogger(value = "修改文章评论状态")
    @PostMapping("/admin/article/update-comment")
    public ResultEntity updateCommentStatus(@RequestParam("articleId") Integer articleId,
                                            @RequestParam("status") Boolean status) {
        boolean res = articleService.updateArticleCommentStatus(articleId, status);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    /**
     * 修改赞赏状态
     *
     * @param articleId 需要修改的文章Id
     * @param status    需要提交的发布状态
     * @return
     */
    @OperationLogger(value = "修改文章赞赏状态")
    @PostMapping("/admin/article/update-appreciation")
    public ResultEntity updateAppreciationStatus(@RequestParam("articleId") Integer articleId,
                                                 @RequestParam("status") Boolean status) {
        boolean res = articleService.updateArticleAppreciationStatus(articleId, status);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    /**
     * 通过批量文章 Id 来删除文章
     *
     * @param articleIds 需要删除的文章Id
     * @return
     */
    @PostMapping("/admin/article/delete")
    public ResultEntity deleteArticle(@RequestParam("articleIds") Integer[] articleIds) {
        boolean res = articleService.updateArticleDeleteStatus(articleIds);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    /**
     * 修改文章内容
     *
     * @param article 需要修改的文章信息
     * @return
     */
    @PostMapping("/admin/article/update")
    public ResultEntity updatePublishStatus(@RequestBody ArticleUpdateVO article) {
        boolean res = articleService.updateArticle(article);
        if (!res) return ResultEntity.error();
        return ResultEntity.ok();
    }

    /**
     * 获取首页的时间排序来获取全部的列表
     *
     * @return
     */
    @GetMapping("/admin/article/list")
    public ResultEntity articleList() {
        List<ArticleVO> articleList = articleService.getAllArticleList();
        return ResultEntity.ok().put("data", articleList);
    }

    /**
     * 获取首页的时间排序来获取全部的列表
     *
     * @return
     */
    @GetMapping("/admin/article/recommend/list")
    public ResultEntity recommendArticleList(@RequestParam("level") Integer level) {
        List<RecommendArticleVO> articleList = articleService.getRecommendListByLevel(level);
        return ResultEntity.ok().put("data", articleList);
    }

    /**
     * 获取首页的时间排序来获取全部的列表
     *
     * @return
     */
    @PostMapping("/admin/article/search")
    public ResultEntity articleSearch(@RequestBody ArticleQueryVO articleQueryVO) {
        List<ArticleVO> articleList = articleService.getArticleListBySearch(articleQueryVO);
        return ResultEntity.ok().put("data", articleList);
    }

    /**
     * 将文章Id列表添加到数据库中
     *
     * @param articleIds
     * @return
     */
    @PostMapping("/admin/subject/article/update")
    public ResultEntity insertArticleToSubject(@RequestParam("articleIds") Integer[] articleIds,
                                               @RequestParam("subjectId") Integer subjectId) {
        return articleService.insertArticleToSubject(articleIds, subjectId);
    }


    /**
     * 将文章移除专题中
     *
     * @param articleId
     * @return
     */
    @PostMapping("/admin/subject/article/delete")
    public ResultEntity deleteArticleFromSubjectById(@RequestParam("articleId") Integer articleId,
                                                     @RequestParam("subjectId") Integer subjectId) {
        return articleService.deleteArticleFromSubjectById(articleId, subjectId);
    }

    /**
     * 更改推荐优先级
     *
     * @return
     */
    @PostMapping("/admin/article/recommend/update")
    public ResultEntity updateRecommendArticleByLevel(@RequestBody List<RecommendArticleVO> recommendList) {
        return articleService.updateBatchRecommendArticle(recommendList);
    }

    /**
     * 修改专题文章的优先级
     *
     * @return
     */
    @PostMapping("/admin/subject/article-priority/update")
    public ResultEntity updateArticlePriority(@RequestBody List<ArticlePriorityVO> articlePriorityList) {
        return articleService.updateArticlePriority(articlePriorityList);
    }


}
